package clazz.ATM;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private Scanner sc=new  Scanner(System.in);
    private ArrayList<Account> s1=new ArrayList<Account>();
    private Account ID;
    public void start() {
        //设立死循环不满足条件会一直在此页面
        while (true) {
            System.out.println("-------欢迎-----");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.print("请输入您的选择:");
            //设立选项
            int a = sc.nextInt();
            switch (a) {
                case 1:
                    login();
                    break;
                case 2:
                    register();
                    break;
                default:
                    System.out.println("没有该操作!");
                    break;
            }
        }
    }
    public void register() {
        Account acc=new Account();
        System.out.println("请输入你的用户名");
        String name=sc.next();
        acc.setName(name);

        while (true) {
            System.out.println("请输入性别");
            char sex = sc.next().charAt(0);
            if (sex == '男' || sex == '女') {
                acc.setSex(sex);
                break;
            } else {
                System.out.println("输入有误");
            }
        }

        while (true) {
            System.out.println("请输入你的账户密码");
            String opassword= sc.next();
            System.out.println("确认你的账户密码");
            String password=sc.next();
            //判断两次密码是否一样
            if (opassword.equals(password)) {
                acc.setPassword(password);
                break;
            }
            else System.out.println("输入不一致");
        }
        System.out.println("请输入限额");
        double limit=sc.nextDouble();
        acc.setLimit(limit);
        System.out.println("恭喜你"+name+"开户成功您的卡号是"+id());
        s1.add(acc);
    }

    public  String id () {

        while (true) {
            Random d = new Random();
            String idcar = "";
            for (int i = 0; i < 8; i++) {
                int date = d.nextInt(10);
                idcar += date;
                Account acc=getid(idcar);
                if (acc==null)
                {
                    return idcar;
                }

            }
        }


    }
    public Account getid(String idcar){

        for (int i = 0; i < s1.size(); i++) {
          Account acc= s1.get(i);//si是数组装着对象里面所有参数
          //判断有没有重复
        if(acc.getIdcar().equals(idcar))//这个号码和对应位置号码是否一致
        {
            return acc;//重复了
        }

        }
        return null;
    }
    public void login()
    {
        System.out.println("请输入当前卡号");
        String D =sc.next();
       if (s1.size()==0)
       {
           System.out.println("目前没有账户");
           start();
       }
       Account scc= getid(D);
       if (scc==null)
       {
           System.out.println("账户不存在");
           start();
       }
       else {
           while (true) {
               System.out.println("请输入密码");
               String M=sc.next();
               if(scc.getPassword().equals(M))
               {
                   ID=scc;
                   System.out.println(scc.getName()+"登陆成功 卡号为"+id());
                scc();
                break;
               }
               else {
                   System.out.println("输入密码错误");
               }
           }
       }

    }
    public void scc(){
        int a=sc.nextInt();
        System.out.println("1.查询账户");
        System.out.println("2.存款");
        System.out.println("3.取款");
        System.out.println("4.转账");
        System.out.println("5.修改密码");
        System.out.println("6.退出");
        System.out.println("7.注销账户");
       switch (a)
       {
        case 1 :{
            showmeassage();}
           case 2:{save();}
           case 3:{takeout();}
           case 4:{transferaccounts();}
           case 5:{changepassword();}
           case 6:{start();}
           case 7:{writeoff();}
        }
    }
        public void showmeassage()
        {
            System.out.println("查询账户当前信息如下");
            System.out.println("号主"+ID.getName()+ID.getSex());
            System.out.println("卡号"+ID.getIdcar());
            System.out.println("余额"+ID.getMoney());
            System.out.println("每次额度"+ID.getLimit());
            System.out.println("按任意键返回");
           String s =sc.next();
            if (s!=null)
            {
                scc();
            }
        }
        public void save()
        {
            System.out.println("请输入你存入的金额");
            double a=sc.nextDouble();
            ID.setMoney(ID.getMoney()+a);
            System.out.println("存款成功当前余额为"+ID.getMoney() );
        }
        public void takeout()
        {
            double s=sc.nextDouble();
        if (s>ID.getMoney())
        {
            System.out.println("你的余额没有这么多钱");
            scc();
        }
        else {
            ID.setMoney(ID.getMoney()-s);
            System.out.println("取款"+s+"元"+"你等余额还剩"+ID.getMoney()+"元");
        }
        }

        public void transferaccounts()
        {

            while (true) {
                System.out.println("欢迎进入转账页面");

                if (s1.size()<=2) {
                    System.out.println("用户少于两人转账失败");
                    scc();


                    if (ID.getMoney() == 0) {
                        System.out.println("余额不足");
                       scc();

                    }
                }
                else {
                    System.out.println("输入转账的卡号");
                    String A=sc.next();
                    Account S=getid(A);
                    if ( getid(A)==null) {
                        System.out.println("卡号不存在");
                        scc();
                    }
                    else {
                        while (true) {
                            System.out.println("是否转账给"+S.getName()+S.getSex());
                            System.out.println("1.确认");
                            System.out.println("2.取消");
                            int s=sc.nextInt();
                            switch (s)
                            {
                                case 1:   System.out.println("请输入转账金额");
                                    double z=sc.nextDouble();
                                    if(ID.getMoney()<s)
                                    {
                                        System.out.println("余额不足");
                                        transferaccounts();
                                    }
                                    else {
                                        S.setMoney(S.getMoney()+z);
                                        ID.setMoney(ID.getMoney()-z);
                                        System.out.println("转账成功"+"转账"+z+"元."+"当前余额"+ID.getMoney()+"元");
                                    }
                                    break;
                                case 2:scc();
                                break;

                            }
                        }
                    }
                }
            }
        }
        public void changepassword()
        {
            System.out.println("请输入当前密码");
            String s=sc.next();
            if(s.equals(ID.getPassword()))
            {
                System.out.println("验证成功请输入新密码");
                String a=sc.next();
                System.out.println("再次输入密码");
                String b=sc.next();
                if (a==b)
                {
                    System.out.println("修改成功");
                    ID.setPassword(b);
                    start();
                }
                else {
                    System.out.println("两次输入不一样");
                    changepassword();
                }
            }
            else {

                        System.out.println("密码错误请重新输入");
                changepassword();
            }
        }
        public void writeoff()
        {
            int a=sc.nextInt();
            switch (a) {
                case 1:
                    if (ID.getMoney() > 0) {
                        System.out.println("账户内还有余额无法注销");
                    } else {
                        s1.remove(ID);
                        start();
                    }
                case 2:
                    System.out.println("好的为你保留账户");
                    scc();
                        }
        }



}
